package com.cbw.firegame.order.web;

import com.alibaba.fastjson.TypeReference;
import com.cbw.common.utils.R;
import com.cbw.common.vo.CategoryEntity;
import com.cbw.common.vo.GameEntity;
import com.cbw.common.vo.MemberEntity;
import com.cbw.firegame.order.entity.OrderDescEntity;
import com.cbw.common.vo.OrderEntity;
import com.cbw.firegame.order.feign.ProductServiceFeign;
import com.cbw.firegame.order.service.OrderDescService;
import com.cbw.firegame.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class ItemOrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderDescService orderDescService;
    @Autowired
    ProductServiceFeign productServiceFeign;

    @RequestMapping("/createorder/{gameId}")
    public String createOrder(@PathVariable("gameId") Long gameId, Model model, HttpSession session){

            R allCategory = productServiceFeign.getAllCategory();
            if (allCategory.getCode() == 0) {
                List<CategoryEntity> data = allCategory.getData(new TypeReference<List<CategoryEntity>>() {});
                System.out.println(data);
                model.addAttribute("categorys", data);
            }


            R r = productServiceFeign.gameInfo(gameId);
            if (r.getCode() == 0) {
                MemberEntity loginUser = (MemberEntity) session.getAttribute("loginUser");
                GameEntity gameInfo = r.getData("game", new TypeReference<GameEntity>() {});
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setCreateTime(new Date());
                orderEntity.setStatus(0);
                orderEntity.setMemberId(loginUser.getId());
                orderEntity.setMemberUsername(loginUser.getUsername());
                orderEntity.setTotalAmount(gameInfo.getPrice());
                orderEntity.setPayAmount(gameInfo.getPrice());
                String orderId = UUID.randomUUID().toString().replace("-", "");
                orderEntity.setOrderId(orderId);
                orderService.save(orderEntity);
                OrderDescEntity orderDescEntity = new OrderDescEntity();
                orderDescEntity.setCatId(gameInfo.getCatId());
                R r1 = productServiceFeign.catInfo(gameInfo.getCatId());
                if (r1.getCode() == 0) {
                    CategoryEntity category = r1.getData("category", new TypeReference<CategoryEntity>() {});
                    orderDescEntity.setCatName(category.getCatName());
                }
                orderDescEntity.setGameId(gameId);
                orderDescEntity.setGameDefaultImg(gameInfo.getGameDefaultImg());
                orderDescEntity.setGameName(gameInfo.getGameName());
                orderDescEntity.setPrice(gameInfo.getPrice());
                orderDescEntity.setOrderId(orderId);
                orderDescService.save(orderDescEntity);
                model.addAttribute("game", gameInfo);
                model.addAttribute("order", orderEntity);
            }

        return "order";
    }


}
